package com.zlc.common.core.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.common.common.service.BaseServiceImpl;
import com.zlc.common.core.process.mapper.StoneProcessAuthConfigMapper;
import com.zlc.common.core.process.model.entity.StoneProcessAuthConfig;
import com.zlc.common.core.process.model.vo.StoneNodeApproveUserConfigVo;
import com.zlc.common.core.process.model.vo.StoneProcessAuthConfigVo;
import com.zlc.common.core.process.service.StoneProcessAuthConfigService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

import static com.zlc.common.common.constant.WorkflowConstant.TYPE_NODE;
import static com.zlc.common.common.constant.WorkflowConstant.TYPE_PROCESS;

/**
 * @author  zl
 * @date 2023-10-17 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoneProcessAuthConfigServiceImpl extends BaseServiceImpl<StoneProcessAuthConfigMapper, StoneProcessAuthConfig, StoneProcessAuthConfigVo> implements StoneProcessAuthConfigService{

    @Override
    public void removeByProcessId(String processId) {
        LambdaQueryWrapper<StoneProcessAuthConfig> wrapper = wrapper()
                .eq(StoneProcessAuthConfig::getProcessId, processId);
        List<StoneProcessAuthConfigVo> nodeVos = queryVoList(wrapper);
        if(!ObjectUtils.isEmpty(nodeVos)){
            List<String> ids = nodeVos.stream().map(StoneProcessAuthConfigVo::getId).collect(Collectors.toList());
            deleteObjByIds(ids);
        }
    }

    @Override
    public List<StoneProcessAuthConfigVo> getProcessAuth(String processId) {
        LambdaQueryWrapper<StoneProcessAuthConfig> wrapper = wrapper()
                .eq(StoneProcessAuthConfig::getProcessId, processId)
                .eq(StoneProcessAuthConfig::getAuthType,TYPE_PROCESS)
                .orderByAsc(StoneProcessAuthConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneProcessAuthConfigVo> getByProcessId(String processId) {
        LambdaQueryWrapper<StoneProcessAuthConfig> wrapper = wrapper()
                .eq(StoneProcessAuthConfig::getProcessId, processId)
                .orderByAsc(StoneProcessAuthConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneProcessAuthConfigVo> getNodeAllowAuth(String processId, String nodeId) {

        LambdaQueryWrapper<StoneProcessAuthConfig> wrapper = wrapper()
                .eq(StoneProcessAuthConfig::getProcessId, processId)
                .eq(StoneProcessAuthConfig::getNodeId,nodeId)
                .eq(StoneProcessAuthConfig::getAuthType,TYPE_NODE)
                .orderByAsc(StoneProcessAuthConfig::getOrder);
        return queryVoList(wrapper);
    }

    @Override
    public List<StoneProcessAuthConfigVo> getProcessAllowAuth(String processId) {
        LambdaQueryWrapper<StoneProcessAuthConfig> wrapper = wrapper()
                .eq(StoneProcessAuthConfig::getProcessId, processId)
                .eq(StoneProcessAuthConfig::getAuthType,TYPE_PROCESS)
                .orderByAsc(StoneProcessAuthConfig::getOrder);
        return queryVoList(wrapper);
    }
}
